package com.kk.netty.nio.file;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

public class TestTraverseDirectory {
    public static void main(String[] args) throws IOException {

    }

    public static void m2() throws IOException {
        // 文件个数
        AtomicInteger fileCount = new AtomicInteger ( );
        // 目录对象
        Path path = Paths.get ("C:\\Program Files\\Java\\jdk1.8.0_202");
        // 设计模式：访问者模式
        Files.walkFileTree (path, new SimpleFileVisitor<Path> ( ) {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                // 根据文件后缀筛选
                if (file.toFile ( ).getName ( ).endsWith (".jar")) {
                    fileCount.incrementAndGet ( );
                }
                return super.visitFile (file, attrs);
            }
        });
        System.out.println (fileCount); // 705
    }

    public static void mi() throws IOException {
        // 目录个数
        AtomicInteger direCount = new AtomicInteger ( );
        // 文件个数
        AtomicInteger fileCount = new AtomicInteger ( );
        // 目录对象
        Path path = Paths.get ("C:\\Program Files\\Java\\jdk1.8.0_202");
        // 设计模式：访问者模式
        Files.walkFileTree (path, new SimpleFileVisitor<Path> ( ) {
            // 获取目录
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println (dir);
                direCount.incrementAndGet ( );
                return super.preVisitDirectory (dir, attrs);
            }

            // 获取文件
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println (file);
                fileCount.incrementAndGet ( );
                return super.visitFile (file, attrs);
            }
        });
        System.out.println (direCount); // 133
        System.out.println (fileCount); // 1479
    }
}
